<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\BaseController;
use App\Models\Site;
use App\Events\ConfigChangeEvent;
use Event;

class SiteController extends BaseController
{
    /**
     * @var 自定义参数填充字段
     */
    protected $groupID3_field = [
        'var_info' => '',
        'var_key' => '',
        'var_value' => '',
        'group_id' => 3
    ];

    /**
     *基本配置页
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //dd(get_config('locale1'));
        //站点系统配置
        $data['site_config'] = Site::where('group_id', '=', '1')->get();
        //用户自定义添加的配置
        $data['custom_config'] = Site::where('group_id', '=', '3')->get();
        return view('admin.site.index', compact('data', $data));
    }

    /**
     * 系统参数页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function sysParam()
    {
        $data['parameter_config'] = Site::where('group_id', '=', '2')->get();
        return view('admin.site.sys_param', compact('data', $data));
    }

    /**
     * 邮件配置页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function email()
    {
        return view('admin.site.email');
    }

    /**
     *添加配置页
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.site.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->all();
        $jumpUrl = $request->get('jumpUrl');
        $groupID = $request->get('group_id');
        if ($groupID == 3) {
            if ($request->get('var_info') == '' || $request->get('var_key') == '' || $request->get('var_value') == '') {
                return back()->withErrors('变量介绍或变量名称或变量值不能为空');
            }
            $site = new Site();
            foreach (array_keys($this->groupID3_field) as $field) {
                $site->$field = $request->get($field);
            }
            $site->save();
            Event::fire(new ConfigChangeEvent());
            return redirect($jumpUrl)->withSuccess('添加系统变量成功！');
        }
        if ($groupID == 2) {
            if ($request->get('debug') == '') {
                $site = Site::where('var_key', '=', 'debug')->first();
                $site->var_value = 'false';
                $site->save();
            }
        }
        foreach ($data as $key => $val) {
            $site = Site::where('var_key', '=', $key)->first();
            if (!$site) continue;
            $site->var_value = $val;
            $site->group_id = $groupID;
            $site->save();
        }
        Event::fire(new ConfigChangeEvent());
        return redirect($jumpUrl)->withSuccess('更新成功！');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
